//
//  AppDelegate.swift
//  XLWB
//
//  Created by MacBook on 16/6/29.
//  Copyright © 2016年 MacBook. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //应用程序额外设置
        setupAdditions()
        
        
        
//        sleep(2)
        
        
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white()
        window?.rootViewController = MainViewController()
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        return true
    }

}
//MARK--设置应用程序额外信息
extension AppDelegate {

    private func setupAdditions() {
    
    
        SVProgressHUD.setMinimumDismissTimeInterval(2)
        
        //设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
    
        // #available 是检测设备版本，如果是 10.0 以上
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .carPlay, .sound]) { (success, error) in
                print("授权 " + (success ? "成功" : "失败"))
            }
        } else {
            // 10.0 以下
            let notifySettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            
           UIApplication.shared().registerUserNotificationSettings(notifySettings)
        }

    }
}
//MARK--从服务器加载应用程序信息
extension AppDelegate {

    private func loadAppInfo() {
    
        let url = Bundle.main.urlForResource("demo.json", withExtension: nil)
        
        let data = NSData(contentsOf: url!)
        
        //写入磁盘
        let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        
        let jsonPath = (docDir as NSString).appendingPathComponent("demo.json")
        
        data?.write(toFile: jsonPath, atomically: true)
        
       print("应用程序加载完毕")
    
    
    
    }






}
